/* ***************************************************************************
 **    global.h
 **
 **  Project      : ClockMatrix_v0
 **  Target       : PIC18F14K50
 **  Description  : Project used to develop clock firmware.  Tested methods and
 **                 determined functionality (needed, wanted, and the possible)
 **
 **  Compiler     : HI-TECH C  Version 9.80
 **  IDE          : MPLABX v1.80
 **  Created      : 5/8/2013
 **  Branched     : 6/27/2013 -- started v2
 **
 **  R Hanlen <implod@gmail.com>
 ** ***************************************************************************/

#ifndef _GLOBAL_H_
#define _GLOBAL_H_

//#include <stdint.h>        /* For uint8_t definition */
//#include <stdbool.h>       /* For true/false definition */
//#include <string.h>


/******************************************************************************/
/* MICRCONTROLLER FOSC                                                        */
/******************************************************************************/


#ifdef _BOOTLOADER
#define _XTAL_FREQ  48000000L
#else
#define _XTAL_FREQ  48000000L
#endif

#define SYS_FREQ    _XTAL_FREQ
#define FCY         SYS_FREQ/4
#define CLOCKCYCLES_PER_MICROSECOND() ( _XTAL_FREQ / 1000000L )
#define CLOCKCYCLES_TO_MICROSECONDS(a) ( ((a) * 1000L) / (_XTAL_FREQ / 1000L) )
#define MICROSECONDS_TO_CLOCKCYCLES(a) ( ((a) * (_XTAL_FREQ / 1000L)) / 1000L )
#define INSTRUCTIONS_PER_MICROSECONDS()     (FCY/1000000L)
#define MICROSECONDS_TO_INSTRUCTIONS(a)   ( ((a) * (FCY / 1000L)) / 1000L )

/******************************************************************************/
/*  Global Macros                                                             */
/******************************************************************************/
#define INPUT 0xFF
#define OUTPUT 0x00
#define ANALOG 0xFF
#define DIGITAL 0x00
#define	HIGH 0xFF
#define	LOW 0x00

#ifndef TRUE
#define TRUE 0x01
#define FALSE 0x00
#endif //TRUE
#define ON  0x01
#define OFF 0x00
#define SET 0x01
#define CLEAR 0x00
#define FULL 0xFF


/******************************************************************************/
/* State Machine Macros                                                        */
/******************************************************************************/

#define TIME_NUMERICAL      0
#define TIME_STRING         1
#define DATE_TIME_STRING    2
#define TEMP_NUMERICAL      3

volatile unsigned char currentState = 0;
volatile unsigned long milliseconds = 0;

volatile unsigned char scrollFlag = 0;
volatile unsigned char i2cFlag = 0;
/******************************************************************************/
/* Hardware                                                                   */
/******************************************************************************/


/*************************SHIFT REGISTERS**************************************/

#define CLK             LATC3
#define DATA            LATC4
#define LATCH           LATC5

#define CLK_TRIS        TRISC3
#define DATA_TRIS       TRISC4
#define LATCH_TRIS      TRISC5

#define CLK_HIGH()      (CLK = SET)
#define CLK_LOW()       (CLK = CLEAR)
#define DATA_HIGH()     (DATA = SET)
#define DATA_LOW()      (DATA = CLEAR)
#define LATCH_HIGH()    (LATCH = SET)
#define LATCH_LOW()     (LATCH = CLEAR)

/*******************************DISPLAY***************************************/

struct rowData {
    unsigned char byte_1;
    unsigned char byte_2;
};

struct displayState {
    struct rowData row[8];
    unsigned char presentRow;
    unsigned char scrollDelay;
} display;

unsigned char displayString[70];
/*************************************I2C**************************************/

#define sDA                 PORTBbits.RB4
#define SDA                 LATB4
#define SDA_TRIS            TRISB4
#define SDA_HI              (SDA_TRIS = INPUT)
#define SDA_LOW             (SDA_TRIS = OUTPUT)

#define sCL                 PORTBbits.RB6
#define SCL                 LATB6
#define SCL_TRIS            TRISB6
#define SCL_HI              (SCL_TRIS = INPUT)
#define SCL_LOW             (SCL_TRIS = OUTPUT)

#define SDA_LAT             SDA
#define SCL_LAT             SCL

struct i2cHardware{
    unsigned char ds1307;
    unsigned char lm75;
} i2cDevices;

/*********************************DS1307**************************************/

 

// typedef struct {
//    unsigned char sec; // BCD for seconds, 00-59
//    unsigned char min; // BCD for minutes, 00-59
//    unsigned char hour; // BCD for hours, 00-24
//    unsigned char wday; // BCD for day of the week, 00-06
//    unsigned char mday; // BCD for day of the month, 01-31
//    unsigned char mon; // BCD for month, 01-12
//    unsigned char year; // BCD for year, 00-99
//    unsigned char cntrl; // Control of sqwave output
//
//} rtc_data;
//
//rtc_data dateTime;

/*********************************BUTTONS_INPUT********************************/

#define UP_BUTTON_TRIS      TRISC2
#define DOWN_BUTTON_TRIS    TRISC0
#define ENTER_BUTTON_TRIS   TRISC1
#define UP_BUTTON           RC2
#define DOWN_BUTTON         RC0
#define ENTER_BUTTON        RC1

#define BUTTON_INTS_ENABLE()      INT0IF = CLEAR;INT1IF = CLEAR;INT2IF = CLEAR;\
                                INT0IE = ON;INT1IE = ON;INT2IE = ON
#define BUTTON_INTS_DISABLE()     INT0IE = OFF;INT1IE = OFF;INT2IE = OFF

#define UP 0
#define DOWN 1
#define ENTER 2
#define DEBOUNCE_AMOUNT	10
/*****************************TMP75*******************************************/



#endif //_GLOBAL_H_
